package com.centerm.util;

import java.util.ArrayList;
import java.util.List;

/**
 * @author linwenhui
 * @version V1.0.0
 * @Title: HexUtil.java
 * @Description: 十六进制转换工具
 * @date 2017/2/8 10:59
 * @created 2017/2/8 10:59
 **/
public class HexUtil {
    /**
     * 功能描述：BCD码转换成阿拉伯数字，例如有数组byte[] data = new byte[]{0x12,0x34,0x56};<br/>
     * 转换为阿拉伯数字字符串后为"123456"
     *
     * @param bytes 所要转换的十进制BCD字节数组
     * @return BCD码表示的阿拉伯数组内容
     */
    public static String bcd2Str(byte[] bytes) {
        StringBuffer temp = new StringBuffer(bytes.length * 2);

        for (int i = 0; i < bytes.length; i++) {
            temp.append((byte) ((bytes[i] & 0xf0) >>> 4));
            temp.append((byte) (bytes[i] & 0x0f));
        }
        return temp.toString().substring(0, 1).equalsIgnoreCase("0") ? temp.toString().substring(1) : temp.toString();
    }

    /**
     * bytes转换成十六进制字符串
     *
     * @param b byte数组
     * @return String 每个Byte值之间空格分隔
     */
    public static String byte2HexStr(byte[] b) {
        if (b == null)
            return "";
        String stmp = "";
        StringBuilder sb = new StringBuilder("");
        for (int n = 0; n < b.length; n++) {
            stmp = Integer.toHexString(b[n] & 0xFF);
            sb.append((stmp.length() == 1) ? "0" + stmp : stmp);
            // sb.append(" ");
        }
        return sb.toString().toUpperCase().trim();
    }

    /**
     * bytes字符串转换为Byte值
     *
     * @param src Byte字符串，每个Byte之间没有分隔符
     * @return byte[]
     */
    public static byte[] hexStr2Bytes(String src) throws NumberFormatException {
        int m, n;
        if ((src.length() % 2) != 0)
            src = "0" + src;
        int l = src.length() / 2;
        byte[] ret = new byte[l];
        for (int i = 0; i < l; i++) {
            m = i * 2 + 1;
            n = m + 1;
            ret[i] = Integer.decode("0x" + src.substring(i * 2, m) + src.substring(m, n))
                    .byteValue();
        }
        return ret;
    }

    public static byte[] toLen(int len) {
        byte[] bytes = hexStr2Bytes(String.valueOf(len));
        byte[] datas = new byte[2];
        datas[0] = bytes.length == 2 ? bytes[0] : (byte) 0x00;
        datas[1] = bytes.length == 2 ? bytes[1] : bytes[0];
        return datas;
    }

    public static byte[] toHexLen(int len) {
        byte[] bytes = hexStr2Bytes(Integer.toHexString(len));
        byte[] datas = new byte[2];
        datas[0] = bytes.length == 2 ? bytes[0] : (byte) 0x00;
        datas[1] = bytes.length == 2 ? bytes[1] : bytes[0];
        return datas;
    }

    public static String toASCII(byte[] bytes) {
        if (bytes == null)
            return "";
        List<Byte> byteList = new ArrayList<>();
        for (byte b : bytes) {
            if (b != 0)
                byteList.add(b);
        }
        final int len = byteList.size();
        if (len == 0)
            return "";
        char[] temps = new char[len];
        for (int i = 0; i < len; i++) {
            temps[i] = (char) byteList.get(i).byteValue();
        }
        return String.valueOf(temps);
    }

    public static String toASCII(String source) {
        final int len = source.length();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < len; i++) {
            buffer.append(String.format("%02x", (int) source.charAt(i)));
        }
        return buffer.toString();
    }

    /* byte to  int */
    public final static int getInt(byte[] buf, boolean asc, int len) {
        if (buf == null) {
            throw new IllegalArgumentException("byte array is null!");
        }
        if (len > 4) {
            throw new IllegalArgumentException("byte array size > 4 !");
        }
        int r = 0;
        if (asc)
            for (int i = len - 1; i >= 0; i--) {
                r <<= 8;
                r |= (buf[i] & 0x000000ff);
            }
        else
            for (int i = 0; i < len; i++) {
                r <<= 8;
                r |= (buf[i] & 0x000000ff);
            }
        return r;
    }

}
